using UnityEngine;
using System.Collections.Generic;
namespace EnhancedUI
{
///
/// This is a super light implementation of an array that
/// behaves like a list, automatically allocating new memory
/// when needed, but not releasing it to garbage collection.
///
/// The type of the list
public class SmallList
{
///
/// internal storage of list data
///
public T[] data;
///
/// The number of elements in the list
///
public int Count = 0;
///
/// Indexed access to the list items
///
///
///
public T this[int i]
{
get { return data[i]; }
set { data[i] = value; }
}
///
/// Resizes the array when more memory is needed.
///
private void ResizeArray()
{
T[] newData;
if (data != null)
newData = new T[Mathf.Max(data.Length << 1, 64)];
else
newData = new T[64];
if (data != null && Count > 0)
data.CopyTo(newData, 0);
data = newData;
}
///
/// Instead of releasing the memory to garbage collection,
/// the list size is set back to zero
///
public void Clear()
{
Count = 0;
}
///
/// Returns the first element of the list
///
///
public T First()
{
if (data == null || Count == 0) return default(T);
return data[0];
}
///
/// Returns the last element of the list
///
///
public T Last()
{
if (data == null || Count == 0) return default(T);
return data[Count - 1];
}
///
/// Adds a new element to the array, creating more
/// memory if necessary
///
///
public void Add(T item)
{
if (data == null || Count == data.Length)
ResizeArray();
data[Count] = item;
Count++;
}
///
/// Adds a new element to the start of the array, creating more
/// memory if necessary
///
///
public void AddStart(T item)
{
Insert(item, 0);
}
///
/// Inserts a new element to the array at the index specified, creating more
/// memory if necessary
///
///
public void Insert(T item, int index)
{
if (data == null || Count == data.Length)
ResizeArray();
for (var i = Count; i > index; i--)
{
data[i] = data[i - 1];
}
data[index] = item;
Count++;
}
///
/// Removes an item from the start of the data
///
///
public T RemoveStart()
{
return RemoveAt(0);
}
///
/// Removes an item from the index of the data
///
///
public T RemoveAt(int index)
{
if (data != null && Count != 0)
{
T val = data[index];
for (var i = index; i < Count - 1; i++)
{
data[i] = data[i + 1];
}
Count--;
data[Count] = default(T);
return val;
}
else
{
return default(T);
}
}
///
/// Removes an item from the data
///
///
///
public T Remove(T item)
{
if (data != null && Count != 0)
{
for (var i = 0; i < Count; i++)
{
if (data[i].Equals(item))
{
return RemoveAt(i);
}
}
}
return default(T);
}
///
/// Removes an item from the end of the data
///
///
public T RemoveEnd()
{
if (data != null && Count != 0)
{
Count--;
T val = data[Count];
data[Count] = default(T);
return val;
}
else
{
return default(T);
}
}
///
/// Determines if the data contains the item
///
/// The item to compare
/// True if the item exists in teh data
public bool Contains(T item)
{
if (data == null)
return false;
for (var i = 0; i < Count; i++)
{
if (data[i].Equals(item))
return true;
}
return false;
}
}
}